package com.symbio.ishenapi.background.controller;


import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.symbio.ishenapi.background.model.TmBackgroundSmsRecord;
import com.symbio.ishenapi.background.sms.SendTelMsgUtils;
import com.symbio.ishenapi.common.controller.BaseController;
import io.jboot.web.controller.annotation.RequestMapping;

import java.util.Date;
import java.util.HashMap;

@RequestMapping("TmBackgroundSmsRecordController")
public class TmBackgroundSmsRecordController extends BaseController {


    public void sendMessage(){
        System.out.print("进入短信发送方法");

        //TmBackgroundSmsRecord tm = (TmBackgroundSmsRecord) getReqBodyObject(TmBackgroundSmsRecord.class);
        String phone = getPara("mobilePhone");
        String orderStatus = getPara("orderStatus");
        String templateId = "260168";
      /*  if(orderStatus.equals("")){
            templateId = "314793";
        }else if(orderStatus.equals("")){
            templateId = "314793";
        }else if(orderStatus.equals("")){
            templateId = "314793";
        }else if(orderStatus.equals("")){
            templateId = "314793";
        }else if(orderStatus.equals("")){
            templateId = "314793";
        }*/
        System.out.print("获取到的手机号码为"+phone);
        if (phone != null && !"".equals(phone)) {
            HashMap<String, Object> result = null;
            //System.out.println("验证码是：" + randNum);
            //String result = sendMsgTo("17611181702");
            //System.out.println(result);
            CCPRestSmsSDK restAPI = new CCPRestSmsSDK();
            String verificationCode= SendTelMsgUtils.createRandNum();
            restAPI.init("sandboxapp.cloopen.com", "8883");// 初始化服务器地址和端口，沙盒环境配置成sandboxapp.cloopen.com，生产环境配置成app.cloopen.com，端口都是8883.
            restAPI.setAccount("8a48b5514a61a814014a70918f4f0896", "03e3102670d242a393bcba0f8dc0323b");// 初始化主账号名称和主账号令牌，登陆云通讯网站后，可在“控制台-应用”中看到开发者主账号ACCOUNT SID和主账号令牌AUTH TOKEN。
            restAPI.setAppId("aaf98f894b353559014b49d7522d0a78");// 初始化应用ID，如果是在沙盒环境开发，请配置“控制台-应用-测试DEMO”中的APPID。如切换到生产环境，请使用自己创建应用的APPID
            result = restAPI.sendTemplateSMS(phone, templateId, new String[]{verificationCode, "2"});
            System.out.println("SDKTestGetSubAccounts result=" + result);
            //插入新增发送记录
            //BaseTmPatientSmsRecord tm = (BaseTmPatientSmsRecord) getReqBodyObject(BaseTmPatientSmsRecord.class);
            String resultstate = (String) result.get("statusCode");
            if ("000000".equals(resultstate)) {
                TmBackgroundSmsRecord tm = new TmBackgroundSmsRecord();
                tm.setSendState(resultstate); //拼接获取
                tm.setVerificationCode(verificationCode);
                tm.setSourceType("test"); //前端获取
                tm.setSmsDetails("【爱肾网】欢迎使用爱肾客户端，您的验证码是" + verificationCode + "请于2分钟内正确输入");
                tm.setCreateTime(new Date());
                if (tm.save()) {////保存成功
                    renderJsonError(40007);
                } else {////保存失败提示信息
                    renderJsonError(40002,"验证码信息");
                }
            } else {/////短信发送失败 返回错误信息
                System.out.print("短信发送失败 返回错误信息"+phone);
                renderJsonError(40008,(String) result.get("statusMsg"));
            }
        }else{////手机号为空返回错误信息
            System.out.print("手机号为空返回错误信息");
            renderJsonError(40003,"手机号码");
        }

    }
}
